import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/gateway/add_gateway/wifi_page.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";
import "package:ttlock_flutter/ttgateway.dart";
import "package:ttlock_flutter/ttlock.dart";

class ListItem extends StatefulWidget{
  final TTGatewayScanModel gateway;
  const ListItem({super.key, required this.gateway});

  @override
  State<StatefulWidget> createState() => _ListItemState();

}

class _ListItemState extends State<ListItem> {

  void _connectGateway(String mac) async {
    EasyLoading.show();
    TTGateway.connect(mac, (status) {
      print("status:>>>> >>>>>>$status");
      if (status == TTGatewayConnectStatus.success) {
        // 配置IP
        _configIp(mac);
      } else {
        EasyLoading.dismiss();
      }
    });
  }

  void _configIp(String mac) {
    try {
      Map paramMap = Map();
      paramMap["mac"] = mac;
      paramMap["type"] = TTIpSettingType.DHCP.index;
      //for static ip setting
      // paramMap["type"] = TTIpSettingType.STATIC_IP.index;
      // paramMap["ipAddress"] = "192.168.1.100";
      // paramMap["subnetMask"] = "255.255.255.0";
      // paramMap["router"] = "192.168.1.1";
      // paramMap["preferredDns"] = "xxx.xxx.xxx.xxx";
      // paramMap["alternateDns"] = "xxx.xxx.xxx.xxx";
      TTGateway.configIp(paramMap, () {
        print("config ip success");
        EasyLoading.dismiss();
        Navigator.push(context, CupertinoPageRoute(builder: (BuildContext context) {
          return WifiPage(mac: mac, gateway: widget.gateway);
        }));
      }, (errorCode, errorMsg) {
        EasyLoading.showToast(errorMsg);
        print('config ip errorCode:$errorCode msg:$errorMsg');
      });
    } catch (error) {
      print(error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        TTGateway.stopScan();
        _connectGateway(widget.gateway.gatewayMac);
      },
      child: Container(
        padding: const EdgeInsets.all(12),
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          color: Colors.white,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Container(
                  height: 35,
                  width: 35,
                  decoration: const BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(50)),
                    color: Colors.white,
                    boxShadow: [BoxShadow(color: ThemeVars.colorBackground, blurRadius: 0, spreadRadius: 3,)],
                  ),
                  child: Center(
                    child: widget.gateway.type == TTGatewayType.g2 ? const Text("G2", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: ThemeVars.colorPrimary),) : const SizedBox(),
                  ),
                ),
                const SizedBox(width: 10,),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(widget.gateway.gatewayName, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
                    const SizedBox(height: 4,),
                    Text(widget.gateway.rssi >= -80 ? "信号强" : "信号弱", style: const TextStyle(fontSize: 12, color: ThemeVars.colorTextTer),),
                  ],
                ),
              ],
            ),
            const Icon(Icons.add, color: ThemeVars.colorPrimary, size: 36,),
          ],
        ),
      ),
    );
  }

}